
<%@page import="model.Category"%>
<%@page import="java.util.ArrayList"%>
<%@page import="model.CategoryService"%>
<%@page import="model.Product"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="model.ProductService"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<% 
String ProductID = "";
String ProductName = "";
String ImageName = "";
int QOH = 0;
double UnitPrice = 0.0;
String CategoryID = "";
if(((HttpServletRequest) request).getAttribute("ProductID") != null) {
    ProductID = ((HttpServletRequest) request).getAttribute("ProductID").toString();
}
if(request.getAttribute("product") != null) {
    Product product = (Product) request.getAttribute("product"); 
    ProductID = product.getProductID();
    ProductName = product.getProductName();
    ImageName = product.getImageName();
    QOH = product.getQOH();
    UnitPrice = product.getUnitPrice();
    CategoryID = product.getCategoryID();
} %>
<ul>
<% if(request.getAttribute("errorMsgs") != null) {
    List errorMsgs = (List) request.getAttribute("errorMsgs");
    Iterator errorMsg = errorMsgs.iterator();
    while(errorMsg.hasNext()) { 
        String error = errorMsg.next().toString(); %>
        <i style='color:red'><li><%=error%></li></i>
<% } } %>
</ul>

<form name="form1" method="post" action="<c:url value="/admin/manage_product.do" />">
  <table width="630" border="0">
    <tr>
      <td width="180"><span class="required">*&nbsp;</span>Mã Sản Phẩm</td>
      <td width="200"><input type="text" name="ProductID" value="<%=ProductID%>"/></td>
      <td rowspan="5"><img src='../images/products/<%=ImageName%>' width=180 height=150/></td>
    </tr>
    <tr>
      <td><span class="required">*&nbsp;</span>Tên Sản Phẩm</td>
      <td><input type="text" name="ProductName" value="<%=ProductName%>"/></td>
    </tr>
    <tr>
      <td><span class="required">*&nbsp;</span>Số Lượng Tồn Kho</td>
      <td><input type="text" name="QOH" value="<%=QOH%>" style="text-align:right"/></td>
    </tr>
    <tr>
      <td><span class="required">*&nbsp;</span>Đơn Giá</td>
      <td><input type="text" name="UnitPrice" value="<%=UnitPrice%>" style="text-align:right"/></td>
    </tr>
    <tr>
      <td><span class="required">*&nbsp;</span>Loại Sản Phẩm</td>
      <td>
      <select name='CategoryID' style="width: 150px;">
            <% 
                CategoryService category_service = new CategoryService();
                ArrayList<Category> lstCategories = category_service.getCategories();
                for(int i = 0; i < lstCategories.size(); i++) {
                    String cid = lstCategories.get(i).getCategoryID();
                    String cname = lstCategories.get(i).getCategoryName(); 
                    if(cid.equals(CategoryID)) {  %>                     
                    <option value="<%=cid%>" selected="true"><%=cname%></option>
               <% } else { %>                   
                    <option value="<%=cid%>"><%=cname%></option>
               <% } } %>
      </select>
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><a href="#">Thêm Loại Sản Phẩm Mới</a></td>
    </tr>
  </table>
  <div style="margin-left: 80px;margin-top: 10px;">
    <input type="submit" class="button" name="btnSubmit" value="Thêm"/>
    <input type="submit" class="button" name="btnSubmit" value="Tìm Kiếm"/>
    <input type="submit" class="button" name="btnSubmit" value="Cập Nhật">
    <input type="submit" class="button" name="btnSubmit" value="Xóa"/></div>
</form>
<hr/>
<table border="1" width="100%" cellpadding="4" cellspacing="0" style="margin-top: 20px;">
<tr bgcolor="#dadddd" align=center><th>#</th><th>Hình</th><th>Mã SP</th><th>Tên SP</th><th>SL Tồn Kho</th><th>Đơn Giá</th><th>Mã Loại</th></tr>

<%   ProductService product_service = new ProductService();
    List lstProducts = product_service.getProducts();
    Iterator products = lstProducts.iterator();
    int j = 0;
    while(products.hasNext()) {
        Product product = (Product) products.next();
        String color = ++j % 2 == 0 ? "#f9e7dd" : "#eec7b7";
        String pid = product.getProductID();
        String pname = product.getProductName();
        String imagename = product.getImageName();
        double unitpricefmt = product.getUnitPrice();
        int qoh = product.getQOH();
        String CID = product.getCategoryID();
%>
	<tr bgcolor="<%=color%>">
		<td align=center><%=j%></td>
		<td align=center><img width=50 title="<%=pname%>" height=50 src="../images/products/<%=imagename%>" /></td>
		<td align=center><%=pid%></td>		
		<td align=center><%=pname%></td>
                <td align=center><%=qoh%></td>
		<td align=right><fmt:formatNumber type="currency" currencySymbol="" maxFractionDigits="0" value="<%=unitpricefmt%>"/></td>
		<td align=center><%=CID%></td>
	</tr>

<% } %>
</table>